<?php
App::uses('AppModel', 'Model');
/**
 * ArticlesEnCascade Model
 *
 * @property article $article_parent
 * @property article $article_enfant
 */
class ArticlesEnCascade extends AppModel {
	function ajouterRegleLigneLigne($id_article_parent, $id_article_enfant, $coefficient) {
		return $this->query("INSERT INTO articles_en_cascades (id_article_parent, id_article_enfant, coef_qtte_parent) VALUES ($id_article_parent, $id_article_enfant, $coefficient)");
	}
	
	// permet de récupérer l'ensemble des règles de type ligne/ligne
	function recupererRegles() {
		$resultat = $this->query('SELECT * FROM articles_en_cascades');
		$out = array();
		
		foreach ($resultat as $ligne) {
			$out[] = $ligne['articles_en_cascades'];
		}
		
		return $out;
	}
	
	// permet de supprimer une règle de type ligne/ligne
	// paramètres :
	// - id_article_parent : l'id de l'article parent à supprimer
	// - id_article_enfant : l'id de l'article enfant à supprimer
	function supprimerRegleLigneLigne($id_article_parent, $id_article_enfant) {
		$this->query("DELETE FROM articles_en_cascades WHERE id_article_parent = $id_article_parent AND id_article_enfant = $id_article_enfant");
	}
	
	// retourne la liste des règles de type ligne/ligne
	function obtenirListe() {
		$resultat = $this->query("SELECT * FROM articles_en_cascades AEC JOIN articles article_parent ON article_parent.id_article = AEC.id_article_parent JOIN articles article_enfant ON article_enfant.id_article = AEC.id_article_enfant");
		$out = array();
		
		foreach ($resultat as $ligne) {
			$out[] = $ligne;
		}
		
		return $out;
	}
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_article_parent';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_article_parent' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_article_enfant' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'coef_qtte_parent' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'article_parent' => array(
			'className' => 'article',
			'foreignKey' => 'id_article',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'article_enfant' => array(
			'className' => 'article',
			'foreignKey' => 'id_article',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
}
